package com.tyrone.leetcode.weekgame;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yelong.xie
 * @date 2024/1/14 10:58
 */
public class BeautifulIndices {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.beautifulIndices("isawsquirrelnearmysquirrelhouseohmy",
                "my",
                "squirrel",
                15));
    }

    static class Solution {
        public List<Integer> beautifulIndices(String s, String a, String b, int k) {
            List<Integer> result = new ArrayList<>();
            int aLen = a.length();
            int bLen = b.length();
            int sLen = s.length();
            for (int i = 0; i < sLen - aLen + 1; i++) {
                String aStr = s.substring(i, i + aLen);
                if (aStr.equals(a)) {
                    int bBegin = Math.max(i - k, 0);
                    int bEnd = Math.min(i + k, sLen);
                    for (int j = bBegin; j <= bEnd && j < (sLen - bLen + 1); j++) {
                        if (s.substring(j, j + bLen).equals(b)) {
                            result.add(i);
                            break;
                        }
                    }
                }
            }
            return result;
        }
    }
}
